package com.yjt.sousou.integral;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.yjt.sousou.Api;
import com.yjt.sousou.R;
import com.yjt.sousou.base.BaseActivity;
import com.yjt.sousou.http.DialogCallback;
import com.yjt.sousou.utils.GetTimeUtil;
import com.yjt.sousou.widget.wheelview.WheelView;
import com.yjt.sousou.widget.wheelview.adapter.NumericWheelAdapter;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：83734
 * 时间：2018/12/1
 * 描述：
 */
public class RankingActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_last_month)
    TextView mTvLastMonth;
    @BindView(R.id.tv_current_time)
    TextView mTvCurrentTime;
    @BindView(R.id.tv_next_month)
    TextView mTvNextMonth;
    @BindView(R.id.rv_ranking)
    RecyclerView mRvRanking;

    private WheelView wl_year, wl_month;
    private PopupWindow popupWindow;
    private String selectDate = "";
    private int currentSelectYear, currentSelectMonth;
    private int clickTime = 0;//点击上个月的次数
    @BindView(R.id.ll_main)
    LinearLayout ll_main;

    private String currentTime;
    private int currentYear;
    private int currentMonth;
    private RankingAdapter mRankingAdapter;
    ArrayList<Ranking.DataBean> mList = new ArrayList<>();
    @Override
    protected Object setContentLayout() {
        return R.layout.activity_ranking;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        mTvTitle.setText(R.string.my_ranking);
        currentTime = GetTimeUtil.getYMTime(System.currentTimeMillis());
        currentYear = Integer.valueOf(currentTime.substring(0,4));
        currentMonth = Integer.valueOf(currentTime.substring(5));
        selectDate = currentTime;
        mTvCurrentTime.setText(currentTime);
        initRecyclerView();
        initData();
    }

    private void initData() {
        String[] split = selectDate.split("-");
        String start = selectDate + "-1";
        String end = selectDate + "-" + GetTimeUtil.getDay(Integer.parseInt(split[0]),Integer.parseInt(split[1]));
        String startTime = GetTimeUtil.getStartTimeByDate(start);
        String endTime = GetTimeUtil.getEndTimeByDate(end);
        OkGo.<Ranking>get(Api.get().getRanking()).tag(this)
                .params("datastart",startTime)
                .params("dataover",endTime)
                .execute(new DialogCallback<Ranking>(Ranking.class,this) {
                    @Override
                    public void onSuccess(Response<Ranking> response) {
                      Ranking ranking = response.body();
                      if(ranking.getReturncode().equals("0")){
                          mRankingAdapter.setNewData(ranking.getData());
                          mRankingAdapter.notifyDataSetChanged();
                      }
                    }
                });
    }

    private void initRecyclerView() {
        final LinearLayoutManager manager = new LinearLayoutManager(this);
        mRvRanking.setLayoutManager(manager);
        mRankingAdapter = new RankingAdapter(R.layout.item_ranking,mList);
        mRvRanking.setAdapter(mRankingAdapter);
    }
    @OnClick({R.id.tv_last_month,R.id.tv_next_month,R.id.tv_current_time})
    void onClick(View view){
        switch (view.getId()){
            case R.id.tv_last_month:
                mTvNextMonth.setTextColor(ContextCompat.getColor(this,R.color.text_last_mouth));
                mTvNextMonth.setClickable(true);
                selectDate = GetTimeUtil.getLastMonth(selectDate, -1);
                mTvCurrentTime.setText(selectDate);
                initData();
                break;
            case R.id.tv_next_month:
                if(!TextUtils.isEmpty(selectDate)){
                    int selectMonth = Integer.valueOf(selectDate.substring(5));
                    int selectYear = Integer.valueOf(selectDate.substring(0,4));
                    if(selectYear == currentYear){
                        if(selectMonth + 1 == currentMonth){
                            mTvNextMonth.setTextColor(ContextCompat.getColor(this,R.color.gray_text));
                            mTvNextMonth.setClickable(false);
                            showToast("超出时间范围");
                        }
                        if(!(selectMonth + 1 > currentMonth)){
                            selectDate = GetTimeUtil.getLastMonth(selectDate,1);
                            mTvCurrentTime.setText(selectDate);
                            initData();
                        }
                    }else if(selectYear < currentYear){
                        selectDate = GetTimeUtil.getLastMonth(selectDate,1);
                        mTvCurrentTime.setText(selectDate);
                        initData();
                    }
                }
                break;
            case R.id.tv_current_time:
               showDatePop();
               makeWindowDark();
                break;
        }
    }
    //选择时间弹窗
    private void showDatePop() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View popupWindowView = inflater.inflate(R.layout.pop_date_select, null);
        TextView tv_confirm =  popupWindowView.findViewById(R.id.tv_confirm);
        TextView tv_cancel =  popupWindowView.findViewById(R.id.tv_cancel);
        popupWindow = new PopupWindow(popupWindowView, mScreenWidth * 4 / 5, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        initWheelView(popupWindowView);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                makeWindowLight();
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentSelectYear = wl_year.getCurrentItem() + 2010;//年
                currentSelectMonth = wl_month.getCurrentItem() + 1;//月
                selectDate = String.valueOf(currentSelectYear) + "-" + String.format("%02d", currentSelectMonth);
                if(currentTime.equals(selectDate)){
                    mTvNextMonth.setTextColor(getResources().getColor(R.color.gray_text));
                    mTvNextMonth.setClickable(false);
                }else{
                    mTvNextMonth.setTextColor(getResources().getColor(R.color.blue_bg));
                    mTvNextMonth.setClickable(true);
                }
                if(currentSelectYear > currentYear){
                    showToast("该日期超出范围");
                }else if(currentSelectYear == currentYear){
                    if(currentSelectMonth > currentMonth){
                        showToast("该日期超出范围");
                    }else{
                        mTvCurrentTime.setText(selectDate);
                        initData();
                        popupWindow.dismiss();
                    }
                }else{
                    mTvCurrentTime.setText(selectDate);
                    initData();
                    popupWindow.dismiss();
                }
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popupWindow.showAtLocation(ll_main, Gravity.CENTER, 0, 0);
    }
    private void initWheelView(View view) {
        Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR);
        int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        wl_year = (WheelView) view.findViewById(R.id.wl_year);
        wl_month = (WheelView) view.findViewById(R.id.wl_month);

        NumericWheelAdapter numericWheelAdapterStart1 = new NumericWheelAdapter(this, 2010, 2100);
        numericWheelAdapterStart1.setLabel("年");
        wl_year.setViewAdapter(numericWheelAdapterStart1);
        numericWheelAdapterStart1.setTextColor(R.color.black);
        numericWheelAdapterStart1.setTextSize(20);
        wl_year.setCyclic(true);//是否可循环滑动
        wl_year.setVisibleItems(5);

        NumericWheelAdapter numericWheelAdapterStart2 = new NumericWheelAdapter(this, 1, 12, "%02d");
        numericWheelAdapterStart2.setLabel("月");
        wl_month.setViewAdapter(numericWheelAdapterStart2);
        numericWheelAdapterStart2.setTextColor(R.color.black);
        numericWheelAdapterStart2.setTextSize(20);
        wl_month.setCyclic(true);
        wl_month.setVisibleItems(5);
        wl_year.setCurrentItem(curYear - 2010);
        wl_month.setCurrentItem(curMonth - 1);
    }
    @Override
    public void onLoadMoreRequested() {

    }
}
